package cn.itcast.com.network.tcp.exercise04;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

/**
 * @author zhangshuai@Cskaoyan.onaliyun.com.
 * @version 1.0
 */
public class Client {

  public static void main(String[] args) throws IOException {
    // 1. 创建Socket对象
    Socket socket = new Socket("localhost",9999);

    OutputStream out = socket.getOutputStream();
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));


    // 2. 传输数据
    BufferedReader br = new BufferedReader(new FileReader("D:\\代码\\代码\\代码\\cn\\itcast\\com\\network\\tcp\\exer04\\src.txt"));
    char[] charBuf = new char[1024];
    int len;

    while ((len = br.read(charBuf)) != -1) {
      bw.write(charBuf, 0, len);
    }

    bw.flush();

    // 接收服务器端发送的反馈消息
    byte[] byteBuf = new byte[1024];
    len = socket.getInputStream().read(byteBuf);
    System.out.println(new String(byteBuf, 0, len));

    // 释放资源
    bw.close();
    socket.close();
  }

}
